<?php
namespace App\Utilities;

use App\Models\UploadPicModel;

class Upload
{
    /**
     * 上传工具类
     */

    /**
     * 上传Excel返回图片地址
     * @return string
     */
    public static function uploadExcel($request,$imgName='excel')
    {
        $uploadSizeLimit = 10 * 1024 * 1023;//限制上传图片尺寸10M
        if($request->hasFile($imgName)){//判断图片存在
            if ($_FILES[$imgName]['size'] > $uploadSizeLimit) {
                echo "<script>alert('文件过大！');history.go(-1);</script>";exit;
            }
            $file = $request->file($imgName);           //获取图片
            return self::uploadLocalExcel($file);         //存储到本地
        } else {
            return '';
        }
    }

    /**
     * 这里是上传到本地
     * 上传方法，并处理文件
     * @return string
     */
    public static function uploadLocalExcel($file)
    {
        if($file->isValid()){
            if (!in_array($file->getClientOriginalExtension(),['xlsx','xls'])) {
                echo "<script>alert('你的文件格式不对！');history.go(-1);</script>";exit;
            }
            $extension       = $file->getClientOriginalExtension() ?: 'png';
            $folderName      = '/uploads/excels/';
            $destinationPath = public_path().$folderName;
            $safeName        = date('YmdHis',time()).rand(10,100).'.'.$extension;
            $file->move($destinationPath, $safeName);
            return $folderName.$safeName;
        } else {
            return "";
        }
    }

    /**
     * 通过接口上传图片到本地，返回图片地址
     * @return string
     */
    public static function uploadImgByApi($request,$imgName='file')
    {
        $uploadSizeLimit = 10 * 1024 * 1023;//限制上传图片尺寸10M
        $suffix_img = [//图片允许后缀
            "png", "jpg", "gif", "bmp", "jpeg", "jpe",
        ];
        if($request->hasFile($imgName)){//判断图片存在
            if ($_FILES[$imgName]['size'] > $uploadSizeLimit) {
                return array('code'=>-2,'msg'=>'图片过大！');
            }
            $file = $request->file($imgName);           //获取图片
            if($file->isValid()){
                if ($file->getClientOriginalExtension() &&
                    !in_array($file->getClientOriginalExtension(), $suffix_img)) {
                    return array('code'=>-3,'msg'=>'图片格式错误！');
                }
                $extension       = $file->getClientOriginalExtension() ?: 'png';
                $folderName      = '/uploads/'.date('Ymd', time()).'/';//图片的目录
                $destinationPath = public_path().$folderName;
                $safeName        = uniqid().'.'.$extension;
                $file->move($destinationPath, $safeName);

                $yuantuImgFilePath=$destinationPath.$safeName;
            ///    $yuantuUrlImg=$folderName.$safeName;

                $md5=md5_file($yuantuImgFilePath);////原图的
                $filePath=UploadPicModel::getFilePath($md5);
                if($filePath){
                    unlink($yuantuImgFilePath);//删除原图
                    return array(
                        'code' => 0,
                        'filePath' =>$filePath[UploadPicModel::TypeSmall],
                        'big' =>$filePath[UploadPicModel::TypeBig],
                    );
                }
                $big=$md5.UploadPicModel::TypeBig.'.'.$extension;///大图的文件名
                $sm=$md5.UploadPicModel::TypeSmall.'.'.$extension;///小图的文件名


                 (new Imgcompress($yuantuImgFilePath,0.7))->compressImg(
                    $destinationPath.$big);

                (new Imgcompress($yuantuImgFilePath,0.2))->compressImg(
                    $destinationPath.$sm);
                unlink($yuantuImgFilePath);//删除原图
                UploadPicModel::addPic($md5,$folderName,$extension);

               //  var_dump($yuantuImgFilePath,$yuantuUrlImg);exit;
                return array(
                    'code' => 0,
                    'filePath' =>$folderName.$sm,
                    'big' =>$folderName.$big,
                );
            }
        } else {
            return array('code'=>-1,'上传错误！');
        }
    }

    /**
     * 通过上传图片到本地，返回图片地址
     * @return string
     */
    public static function uploadImg($request,$imgName='file')
    {
        $uploadSizeLimit = 10 * 1024 * 1023;//限制上传图片尺寸10M
        $suffix_img = [//图片允许后缀
            "png", "jpg", "gif", "bmp", "jpeg", "jpe",
        ];
        if($request->hasFile($imgName)){//判断图片存在
            if ($_FILES[$imgName]['size'] > $uploadSizeLimit) {
                return array('code'=>-2,'msg'=>'图片过大！');
            }
            $file = $request->file($imgName);           //获取图片
            if($file->isValid()){
                if ($file->getClientOriginalExtension() &&
                    !in_array($file->getClientOriginalExtension(), $suffix_img)) {
                    return array('code'=>-3,'msg'=>'图片格式错误！');
                }
                $extension       = $file->getClientOriginalExtension() ?: 'png';
                $folderName      = '/uploads/'.date('Ymd', time()).'/';
                $destinationPath = public_path().$folderName;
                $safeName        = uniqid().'.'.$extension;
                $file->move($destinationPath, $safeName);
                return array(
                    'code' => 0,
                    'filePath' => $folderName.$safeName,
                );
            }
        } else {
            return array('code'=>-1,'上传错误！');
        }
    }
}